from langchain.chains import RetrievalQA
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.callbacks.manager import CallbackManager
from langchain_community.llms import Ollama
from langchain_community.embeddings.ollama import OllamaEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import DirectoryLoader
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory
import os
import time
import pandas as pd
from langchain_community.document_loaders import CSVLoader

with open("modelname.cfg","r") as f:
    modelname=f.readline().strip()
print("using llm modle",modelname)

csvname="files/kcjlczycsv.csv"
df = pd.read_excel('files/上课反馈czy.xlsx')
df['上课时间'] = pd.to_datetime(df['上课时间'])
df['结束时间'] = pd.to_datetime(df['结束时间'])

df.to_csv(csvname, encoding ='utf-8_sig')
loader = CSVLoader(csvname,autodetect_encoding=True)
csvdocs = loader.load()
for idx,csvdoc in enumerate(csvdocs):
    rtime=df.loc[idx,"上课时间"]
    csvdoc.metadata["year"]=rtime.strftime("%Y")
    csvdoc.metadata["month"]=rtime.strftime("%m")
    csvdoc.metadata["day"]=rtime.strftime("%d")
    csvdoc.metadata["date"]=rtime.strftime("%Y-%m-%d")
    csvdoc.metadata["quater"]=(rtime.month-1)//4+1
    csvdoc.metadata["student"]=df.loc[idx,"学生姓名"]
    csvdoc.metadata["school"]=df.loc[idx,"上课校区"]
    csvdoc.metadata["duration"]=df.loc[idx,"课时消耗"]
    print(csvdoc)

print("store vector in chroma db ，embedding...")
vectorstore = Chroma.from_documents(persist_directory='jj',
    documents=csvdocs,
    embedding=OllamaEmbeddings(model=modelname)
)
print("embeddong done")
vectorstore.persist()
print("vector store done with model's help",modelname)





